
// pinia 定义公共状态和方法
// 选项式
// 1. defineStore('counter', options)

import { defineStore } from "pinia";

export let useDemoStore = defineStore("demo",{
    state(){
        return {
            a:1,
            b:2,
        }
    },
    getters:{
        doubleA(){
            return this.a * 2
        },
        doubleB(){
            return this.b * 2
        },
        both(){
            return this.a + this.b;
        },
        doubleBoth(){
            return this.doubleA + this.doubleB;
        }
    },
    actions:{
        async addA(payload){
            console.log("payload",payload);
            this.a++;
        },
        addB(payload){
            this.b++;
        },
        addBoth(payload){
            this.addA();
            this.addB();
        },
        async addA_Async(){
            return new Promise((resolve,reject)=>{
                setTimeout(()=>{
                    this.a++;
                    resolve();
                },1000);
            })
        },
        async addB_Async(){
            return new Promise((resolve,reject)=>{
                setTimeout(()=>{
                    this.b++;
                    resolve();
                },2000);
            })
        },
        async addBoth_Async(){
            await this.addA_Async()
            console.log(11111);
            await this.addB_Async();
            console.log(22222);
        }       
    }
}) 